<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<c:set var="shopId" value="${param.shopId}"/>
<c:set var="detail" value="${param.detail}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/js/xtable.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/ContractRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/ShopRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/MarketRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<center>
    <table style="width: 50%;" cellpadding="5" cellspacing="5">
        <tr>
            <td>
                <select id="marketId" name="marketId" onchange="getShopList()">
                </select>
            </td>
            <td>
                <select id="shopId" name="shopId" onchange="getContractPage()">
                </select>
            </td>
            <c:if test="${detail != '1'}">
                <td>
                    <button type="button" onclick="submitContract('')">添加合同</button>
                </td>
            </c:if>
            <c:if test="${detail == '1'}">
                <td>
                    <button type="button" onclick="callBackHistory('')">返回</button>
                </td>
            </c:if>
        </tr>
    </table>
</center>
<SCRIPT LANGUAGE="JavaScript" >
    var contractThead = new Array(
        new XThead("合同ID"),
        new XThead("商户姓名"),
        new XThead("商户公司"),
        new XThead("起始日期"),
        new XThead("截止日期"),
        new XThead("状态"),
        new XThead("操作")
    );

    var contractXTable = new XTable("contract", contractThead, true);

    //    是否有效
    var state_hash = {
        "0": "无效",
        "1": "有效"
    }


    contractXTable.XTbodyArray = function (model) {
        var array = new Array(
                new XTbody("contract", model.contractId, "string", null, null, "center"),
                new XTbody("contract", model.shop.shopLegalName, "string", null, null, "center"),
                new XTbody("contract", model.shop.shopName, "string", null, null, "center"),
                new XTbody("contract", model.contractStartDate.Format("yyyy-MM-dd"), "string", null, null, "center"),
                new XTbody("contract", model.contractEndingDate.Format("yyyy-MM-dd"), "string", null, null, "center"),
                new XTbody("contract", state_hash[model.contractState], "string", null, null, "center"),
                new XTbody("contract", (('${detail}' !='1') ? "<a href='#' style='border-right: 1px solid #dddddd' onclick=submitContract('" + model.contractId + "')>编辑&nbsp;</a>" : "") +
                        "<a href='#' onclick=manageContract('" + model.contractId + "')>&nbsp;管理明细</a>", "string", null, null, "center")
        );
        return array;
    }


    function getContractPage() {
        var contract = new Contract();
        contract.shopId = dwr.util.getValue("shopId");
        contract.marketId = dwr.util.getValue("marketId");
        ContractRpcService.selectContractList(contract, function (list) {
            console.log(list)
            contractXTable.addTable(list);
        });
    }
</SCRIPT>
</body>
</html>
<script>
    //    显示市场列表
    var market = new Market();
    market.marketId = "${marketId}";
    MarketRpcService.selectMarketList(market, callbackSelectMarketList);

    function callbackSelectMarketList(list) {
        dwr.util.addOptions("marketId", list, "marketId", "marketName");
        dwr.util.setValue("marketId", "${marketId}");
        getShopList();
        getContractPage();
    }

    function getShopList(){
        var shop = new Shop();
        shop.marketId = dwr.util.getValue("marketId");
        shop.shopState = "1";
        ShopRpcService.selectShopList(shop, callbackSelectShopList);
    }

    function callbackSelectShopList(list) {
        dwr.util.removeAllOptions("shopId");
        dwr.util.addOptions("shopId", {"" : "请选择"});
        dwr.util.addOptions("shopId", list, "shopId", "shopName");
        if('${shopId}' != "" && '${shopId}'.length > 0){
            dwr.util.setValue("shopId", "${shopId}");
        }
        getContractPage();
    }

    function submitContract(contractId){
        location = "${ctx}/contract/contract_manage.jsp?contractId=" + contractId;
    }

    function manageContract(contractId){
        location = "${ctx}/contract/contract_region_list.jsp?contractId=" + contractId+"&detail=" + '${detail}';
    }

    function callBackHistory(){
        location = "${ctx}/shop/shop_list.jsp";
    }
</script>
